package com.foreveross.fetl.web.action.preprocess;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.foreveross.fetl.application.ILpFreightApplication;
import com.foreveross.fetl.application.ISourceApplication;
import com.opensymphony.xwork2.ActionSupport;

@Controller
public class GenerateDataAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3945805235894598727L;
	@Inject
	private ILpFreightApplication lpFreightApplication;
	
	private Map<String, Object> dataMap = null;

	private Long channelId;
	
	public Long getChannelId() {
		return channelId;
	}

	public void setChannelId(Long channelId) {
		this.channelId = channelId;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public String gen() throws Exception {
		dataMap = new HashMap<String, Object>();
		try{
			lpFreightApplication.randomFreight(channelId);
			dataMap.put("result", Boolean.TRUE);
		}catch(Exception e){
			e.printStackTrace();
			dataMap.put("result", Boolean.FALSE);
		}
		return "JSON";
	}
	
	public String show() throws Exception {
		ServletActionContext.getRequest().setAttribute("channels", 
				com.foreveross.fetl.common.InstanceFactory.getInstance(ISourceApplication.class).listAll());
		return "METHOD";
	}
	
	public String loadChannels() throws Exception {
		dataMap = new HashMap<String, Object>();
		try{
			dataMap.put("result", com.foreveross.fetl.common.InstanceFactory
					.getInstance(ISourceApplication.class).listAll());
		}catch(Exception e){
			e.printStackTrace();
			dataMap.put("result", Boolean.FALSE);
		}
		return "JSON";
	}
}
